package com.twu.biblioteca;


import java.util.ArrayList;
import java.util.Dictionary;
import java.util.Hashtable;
import java.util.concurrent.Callable;

public class Library {
    LoginManager loginManager;
    private static ArrayList<RentableObject> rentableObjects=new ArrayList<RentableObject>();
    Hashtable<RentableObject,Customer> checkedOutList;
    static {
        rentableObjects.add(new Book("B1","Head First Java", "Bert Bates and Kathy Sierra", 2004));
        rentableObjects.add(new Book("B2","Head First Design Pattern", "Kathy Sierra and team", 2004));
        rentableObjects.add(new Book("B3","Effective Java", "Joshua Bloch", 2001));
        rentableObjects.add(new Book("B4","Java Concurrency in Practice", "Joshua Bloch, Doug Lea and team", 2006));
        rentableObjects.add(new Book("B5","Java Generics and Collection", "Naftalin and Philip Wadler", 2006));
        rentableObjects.add(new Movie("M1","Schindler's List", " Steven Spielberg", 1993,8));
        rentableObjects.add(new Movie("M2","The Shawshank Redemption", " Frank Darabont", 1994,9));
        rentableObjects.add(new Movie("M3","Blood Diamond", "Edward Zwick",2006 ,8));
        rentableObjects.add(new Movie("M4","Cast Away", "Robert Zemeckis",2000 ,7));
        rentableObjects.add(new Movie("M5","The Matrix Reloaded", "The Wachowski Brothers",2003 ,8));
    }

    Library(LoginManager loginManager){
        checkedOutList= new Hashtable<RentableObject,Customer>();
        this.loginManager=loginManager;
    }

    public boolean isAvailable(String id){
        RentableObject object=getRentableObject(id);
        return object != null && !object.getCheckedOut();
    }

    public String listBooks(){
        String list = null;
        for (RentableObject rentableObject : rentableObjects) {
            if (String.valueOf(rentableObject.getClass()).equals("class com.twu.biblioteca.Book")) {
                if (isAvailable(rentableObject.getId())) {
                    if (list == null)
                        list = (rentableObject.getDetails());
                    else
                        list = list.concat("\n" + rentableObject.getDetails());
                }
            }
        }
        return list;
    }

    public String listMovies() throws Exception {
        String list = null;
        for (RentableObject rentableObject : rentableObjects)
        {
            if(String.valueOf(rentableObject.getClass()).equals("class com.twu.biblioteca.Movie")) {
                if (isAvailable(rentableObject.getId())) {
                    if (list == null)
                        list = (rentableObject.getDetails());
                    else
                        list = list.concat("\n" + rentableObject.getDetails());
                }
            }
        }
        return list;
    }

    RentableObject getRentableObject(String id) {
        RentableObject rentableObject=null;
        for (RentableObject Object : rentableObjects) {
            if (id.equals(Object.getId()))
                rentableObject=Object;
        }
        return rentableObject;
    }


    public RentableObject checkOutAnObject(String id) throws Exception {
        if (isAvailable(id)) {
            RentableObject object = getRentableObject(id);
            Customer onlineCustomer=loginManager.getOnlineCustomer();
            object.markCheckedOut();
            checkedOutList.put(object, onlineCustomer);
            return object;
        }
        throw new InvalidTitleException();
    }


    public void returnObject(String id) throws Exception {
            RentableObject object = getRentableObject(id);
            if (object != null){
                if(object.getCheckedOut()) {
                    Customer onlineCustomer = loginManager.getOnlineCustomer();
                    if (onlineCustomer == checkedOutList.get(object)) {
                        checkedOutList.remove(object);
                        object.markCheckedOut();
                    } else
                        throw new InvalidObjectException();
                }else
                    throw new InvalidObjectException();
            }else
                throw new InvalidTitleException();
    }

}
